package com.samoiloff.util.data {
	import Array;
	import Boolean;
	import Function;
	import Object;
	import String;
	import int;
	import com.samoiloff.util.data.ArrItem;
	import com.samoiloff.util.events.ArrItemEvent;

	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;

	/**
	 * @author Damirr
	 */
	public class ArrItemDispatcher extends ArrItem implements IEventDispatcher {
		
		private var _dispatcher: EventDispatcher;

		public function ArrItemDispatcher() {
			super();
			_dispatcher = new EventDispatcher();
		}
		
		public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int =0, useWeakReference:Boolean = false):void {
			_dispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference);
		}
		
		public function dispatchEvent(e:Event):Boolean {
			return _dispatcher.dispatchEvent(e);
		}
		
		public function hasEventListener(type: String): Boolean {
			return _dispatcher.hasEventListener(type);
		}
		
		public function willTrigger(type: String): Boolean {
			return _dispatcher.willTrigger(type);
		}
		
		public function removeEventListener(type: String, listener: Function, useCapture: Boolean = false): void {
			_dispatcher.removeEventListener(type, listener, useCapture);
		}
		
		
		override public function setItemAt(i:int, item:Object):void {
			super.setItemAt(i, item);
			dispatchEvent(new ArrItemEvent(ArrItemEvent.ITEM_SET, item));
		}
		
		override public function removeItem(item:Object):Object {
			var obj:Object = super.removeItem(item);
			if (obj) {
				dispatchEvent(new ArrItemEvent(ArrItemEvent.ITEM_REMOVE, obj));
			}
			return obj;
		}
		
		override public function set source(arr:Array):void {
			super.source = arr;
			dispatchEvent(new ArrItemEvent(ArrItemEvent.SOURCE_CHANGE));
		}
		
		override public function addItem(item:Object):void {
			super.addItem(item);
			dispatchEvent(new ArrItemEvent(ArrItemEvent.ITEM_ADD, item));
		}
		
	}
}
